package com.simtop.hj.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.hj.common.ServerResponse;
import com.simtop.hj.mapper.QuestionMapper;
import com.simtop.hj.pojo.*;
import com.simtop.hj.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {


    @Autowired
    public QuestionMapper questionMapper;

    @Override
    public ServerResponse getQuestion(int[] questionIds,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Answer> answerList=questionMapper.getAnswer();//获取回答的基础信息
        List<Question> questionResult=new ArrayList<>();
        Arrays.sort(questionIds);
        for (int i = 0; i < questionIds.length; i++) {
            PageHelper.startPage(pageNum,pageSize);
            Question questionList=questionMapper.getQuestion(questionIds[i]);//获取到题目和回答的选项
            questionResult.add(questionList);
        }
        PageInfo<Answer> answerListPageInfo=new PageInfo(answerList);
        PageInfo<Question> questionResultPageInfo=new PageInfo(questionResult);
        QuestionResult result=new QuestionResult(answerListPageInfo,questionResultPageInfo);
        return ServerResponse.createBySuccess(result);
    }

    @Override
    public ServerResponse provinceStatistics() {
        List<ProvinceResult> provinceResultList=questionMapper.provinceStatistics();
        return ServerResponse.createBySuccess(provinceResultList);
    }

    @Override
    public ServerResponse doPicture(Integer questionId,Integer category) {
        /**
         * 对象格式
         */
        PictureValue pictureValue=new PictureValue();
        Question question=questionMapper.getQuestionName(questionId);
        List<AnswerValue> answerValueList=questionMapper.getAnswerValue(questionId);
        pictureValue.setAnswerValueList(answerValueList);
        pictureValue.setPart(question.getPart());
        pictureValue.setQuestionName(question.getQuestionName());

        /**
         * 数组格式
         */
        PictureArrayValue pictureArrayValue=new PictureArrayValue();
        StringBuilder answerDetailSb=new StringBuilder();
        StringBuilder numsSb=new StringBuilder();
        for (AnswerValue answerValue : answerValueList) {
            answerDetailSb.append(answerValue.getAnswerDetail()).append(",");
            numsSb.append(answerValue.getNums()).append(",");
        }
        pictureArrayValue.setAnswerDetailArray(answerDetailSb.toString().split(","));
        pictureArrayValue.setNumsArray(numsSb.toString().split(","));
        pictureArrayValue.setPart(question.getPart());
        pictureArrayValue.setQuestionName(question.getQuestionName());
        if(category==1) {
            return ServerResponse.createBySuccess(pictureValue);
        }else {
            return ServerResponse.createBySuccess(pictureArrayValue);
        }

    }

    @Override
    public ServerResponse commitPartOneGrade(Double grade, Integer userId) {
        QuestionGrade questionGrade=new QuestionGrade();
        questionGrade.setUserId(userId);
        questionGrade.setPartOneGrade(grade);
        Integer commitPartOneGradeStatus=questionMapper.commitPartOneGrade(questionGrade);
        if(commitPartOneGradeStatus!=0){
            return ServerResponse.createBySuccess("成绩提交成功",questionGrade.getQuestionGradeId());
        }else{
            return ServerResponse.createByErrorMsg("成绩提交失败");
        }
    }


    @Override
    public ServerResponse commitPartGrade(Double grade, Integer gradeCategory, Integer questionGradeId) {
        QuestionGrade questionGrade=new QuestionGrade();
        questionGrade.setQuestionGradeId(questionGradeId);

        if(gradeCategory==2){
            questionGrade.setPartSecondGrade(grade);
        }else if(gradeCategory==3){
            questionGrade.setPartThirdGrade(grade);
        }else{
            questionGrade.setPartFourGrade(grade);
        }
        System.out.println(questionGrade);
        Integer commitPartGradeStatus=questionMapper.commitPartGrade(questionGrade);
        if(commitPartGradeStatus!=0){
            return ServerResponse.createBySuccessMsg("成绩添加成功");
        }else{
            return ServerResponse.createByErrorMsg("成绩添加失败");
        }
    }


    @Override
    public ServerResponse getModuleGrade(User user,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<QuestionGrade> questionGradeList=questionMapper.getModuleGrade(user);
        PageInfo<QuestionGrade> questionGradePageInfo=new PageInfo<>(questionGradeList);
        return ServerResponse.createBySuccess(questionGradePageInfo);
    }

    @Override
    public ServerResponse getWineBrand(Integer choose) {
        List<PriceData> priceDataList=questionMapper.getWineBrand(choose);
        return ServerResponse.createBySuccess(priceDataList);
    }


    @Override
    public ServerResponse getTendency(String[] brandNameArray) {
        List<NormalData> normalDataList=new ArrayList<>();
        /**
         * 获取到十二个月份得数据趋势
         *
         */
        for (int i = 0; i < brandNameArray.length; i++) {
            List<PriceData> priceDataList=questionMapper.getTendency(brandNameArray[i]);
            NormalData normalData=new NormalData();
            normalData.setBrandName(priceDataList.get(0).getChangeName());
            priceDataList.forEach(priceData -> {
                StringBuilder sb=new StringBuilder();
                sb.append(priceData.getJanPrice()).append(",");
                sb.append(priceData.getFebPrice()).append(",");
                sb.append(priceData.getMarPrice()).append(",");
                sb.append(priceData.getAprPrice()).append(",");
                sb.append(priceData.getMayPrice()).append(",");
                sb.append(priceData.getJunPrice()).append(",");
                sb.append(priceData.getJulPrice()).append(",");
                sb.append(priceData.getAugPrice()).append(",");
                sb.append(priceData.getSepPrice()).append(",");
                sb.append(priceData.getOctPrice()).append(",");
                sb.append(priceData.getNovPrice()).append(",");
                sb.append(priceData.getDecPrice());
                normalData.setValue(sb.toString().split(","));
            });
            normalDataList.add(normalData);
        }
        return ServerResponse.createBySuccess(normalDataList);
    }

    @Override
    public ServerResponse getWineElement(String[] brandNameArray) {
        ResultNormalData resultNormalData=new ResultNormalData();
        List<NormalData> lowSeasonList=new ArrayList<>();
        List<NormalData> commerceList=new ArrayList<>();
        List<NormalData> festivalList=new ArrayList<>();
        for (int i = 0; i < brandNameArray.length; i++) {
            List<PriceData> priceDataList=questionMapper.getTendency(brandNameArray[i]);
            NormalData lowSeasonNormalData=new NormalData();
            NormalData commerceNormalData=new NormalData();
            NormalData festivalNormalData=new NormalData();
            lowSeasonNormalData.setBrandName(priceDataList.get(0).getChangeName());
            commerceNormalData.setBrandName(priceDataList.get(0).getChangeName());
            festivalNormalData.setBrandName(priceDataList.get(0).getChangeName());
            priceDataList.forEach(priceData -> {
                StringBuilder lowSeasonSb=new StringBuilder();
                StringBuilder commerceSb=new StringBuilder();
                StringBuilder festivalSb=new StringBuilder();
                lowSeasonSb.append(priceData.getProductPrice()).append(",").append(priceData.getHighSeasonPrice()).append(",").append(priceData.getHighSeasonPrice());
                commerceSb.append(priceData.getProductPrice()).append(",").append(priceData.getSixEightPrice()).append(",").append(priceData.getDoubleNovPrice());
                festivalSb.append(priceData.getProductPrice()).append(",").append(priceData.getSpringFestivalPrice()).append(",").append(priceData.getNationalDayPrice());
                lowSeasonNormalData.setValue(lowSeasonSb.toString().split(","));
                commerceNormalData.setValue(commerceSb.toString().split(","));
                festivalNormalData.setValue(festivalSb.toString().split(","));
            });
            lowSeasonList.add(lowSeasonNormalData);
            commerceList.add(commerceNormalData);
            festivalList.add(festivalNormalData);
        }
        resultNormalData.setLowSeasonList(lowSeasonList);
        resultNormalData.setCommerceList(commerceList);
        resultNormalData.setFestivalList(festivalList);
        return ServerResponse.createBySuccess(resultNormalData);
    }


    @Override
    public ServerResponse getWineAvg(String[] brandNameArray, String productCategory) {
        AvgPrice avgPrice=questionMapper.getWineAvg(productCategory,brandNameArray);
        return ServerResponse.createBySuccess(avgPrice);
    }


    @Override
    public ServerResponse calculatePrice(String brandName,int[] months) {
        int[] monthsNumber=new int[2];
       List<PriceData> priceDataList=questionMapper.getTendency(brandName);
       PriceData priceData=priceDataList.get(0);
       double avg=(priceData.getJanPrice()+priceData.getFebPrice()+priceData.getMarPrice()+
               priceData.getAprPrice()+priceData.getMayPrice()+priceData.getJunPrice()+
               priceData.getJulPrice()+priceData.getAugPrice()+priceData.getSepPrice()+
               priceData.getOctPrice()+priceData.getNovPrice()+priceData.getDecPrice())/12;
        for (int i = 0; i <months.length; i++) {
            switch (months[i]){
                case 1:monthsNumber[i]=priceData.getJanPrice();break;
                case 2:monthsNumber[i]=priceData.getFebPrice();break;
                case 3:monthsNumber[i]=priceData.getMarPrice();break;
                case 4:monthsNumber[i]=priceData.getAprPrice();break;
                case 5:monthsNumber[i]=priceData.getMayPrice();break;
                case 6:monthsNumber[i]=priceData.getJunPrice();break;
                case 7:monthsNumber[i]=priceData.getJulPrice();break;
                case 8:monthsNumber[i]=priceData.getAugPrice();break;
                case 9:monthsNumber[i]=priceData.getSepPrice();break;
                case 10:monthsNumber[i]=priceData.getOctPrice();break;
                case 11:monthsNumber[i]=priceData.getNovPrice();break;
                case 12:monthsNumber[i]=priceData.getDecPrice();break;
                default:break;
            }
        }
        double var=(Math.pow(monthsNumber[0]-avg,2)+Math.pow(monthsNumber[1]-avg,2))/2;
        double deviation=Math.sqrt(var);
        CalculateResult calculateResult=new CalculateResult();
        calculateResult.setVariance((int) var);
        calculateResult.setDeviation((int) deviation);
        return ServerResponse.createBySuccess(calculateResult);
    }

}
